//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.weilai.dao;

import com.weilai.domain.Comments;
import com.weilai.domain.Likes;
import com.weilai.domain.Posts;

import java.util.List;


import org.apache.ibatis.annotations.*;

@Mapper
public interface PostsDao {
    //按id查询
    @Select("select * from posts where id=#{id}")
    Posts selectById(int id);
    //按userId查询
    @Select("select * from posts where user_id=#{id}")
    List<Posts> selectByUserId(int id);
    //添加帖子
    @Insert({"insert into posts values (null,#{userId},#{title},#{content},#{type},'审核中'," +
            "#{categoryId},#{categoryName},#{imageUrl},0,0,0,0,#{createdAt},#{updatedAt})"})
    void add(Posts posts);
    //删除帖子
    @Delete({"delete from posts where id=#{id}"})
    void deleteById(int id);
    //按userId删除帖子
    @Delete({"delete from posts where user_id=#{userId}"})
    void deleteByUserId(int userId);
    //修改帖子
    @Update({"update posts set title=#{title},content=#{content},type=#{type},category_id=#{categoryId},category_name=#{categoryName} where id=#{id}"})
    void update(Posts posts);
    //查询所有帖子
    @Select("select * from posts where status='已发布' order by created_at desc")
    List<Posts>selectAll();
    //查询别人所有文章
    @Select({"select * from posts where type='文章' and status='已发布' and user_id=#{userId} order by created_at desc"})
    List<Posts> selectAllArticle(int userId);
    //查询自己所有文章
    @Select({"select * from posts where type='文章' and user_id=#{userId} order by created_at desc"})
    List<Posts> selectMyAllArticle(int userId);
    //查询别人所有讨论
    @Select({"select * from posts where type='讨论' and status='已发布' and user_id=#{userId} order by created_at desc"})
    List<Posts> selectAllDiscussion(int userId);
    //查询自己所有讨论
    @Select({"select * from posts where type='讨论' and user_id=#{userId} order by created_at desc"})
    List<Posts> selectMyAllDiscussion(int userId);
    //根据标题关键字查询帖子
    @Select({"select * from posts where title like '%#{title}%'"})
    List<Posts> selectByTitle(String title);
    //根据时间查询帖子
    @Select({"select * from posts where created_at between #{date1} and #{date2}"})
    List<Posts> selectByDate(String date1, String date2);
    // 评论数增加1
    @Update("update posts set comment_count = comment_count + 1 where id = #{postId}")
    void incrementCommentCount(int postId);
    // 浏览数增加1
    @Update("update posts set view_count = view_count + 1 where id = #{postId}")
    void incrementViewCount(int postId);
    // 转发数增加1
    @Update("update posts set share_count = share_count + 1 where id = #{postId}")
    void incrementShareCount(int postId);
    // 点赞数增加1
    @Update("update posts set like_count = like_count + 1 where id = #{postId}")
    void incrementLikeCount(int postId);
    // 插入评论记录
    @Insert("insert into comments (user_id, post_id, content,created_at) values (#{userId}, #{postId}, #{content},#{createdAt})")
    void insertCommentRecord(Comments comments);
    // 查询指定帖子的所有评论
    @Select("select * from comments where post_id=#{postId} and status='已发布' order by created_at desc")
    List<Comments> getCommentsByPostId(int postId);
    // 插入点赞记录
    @Insert("insert into likes (user_id, post_id) values (#{userId}, #{postId})")
    void insertLikeRecord(int userId, int postId);
    // 检查用户是否已点赞
    @Select("select count(*) from likes where user_id = #{userId} and post_id = #{postId} and is_deleted = 0")
    int countUserLiked(int userId, int postId);
    // 根据用户id和帖子id查询点赞记录
    @Select("select * from likes where user_id = #{userId} and post_id = #{postId} and is_deleted = 0")
    Likes selectLikeRecord(int userId, int postId);
    // 更新点赞记录为已取消（将is_deleted设为1 ）
    @Update("update likes set is_deleted = 1 where id = #{likeId} and user_id=#{userId}")
    void cancelLikeRecord(int likeId,int userId);
    // 点赞数减少1
    @Update("update posts set like_count = like_count - 1 where id = #{postId}")
    void decrementLikeCount(int postId);
    @Delete("delete from likes where user_id=#{userId} and post_id=#{postId} and is_deleted = 1")
    void delete(int userId,int postId);
    //获取当前点赞数
    @Select("SELECT like_count FROM posts WHERE id = #{postId}")
    int getLikeCount(int postId);
    // 根据评论id和用户id删除评论
    @Delete("DELETE FROM comments WHERE id = #{commentId} AND user_id = #{userId}")
    int deleteCommentByUser(int commentId, int userId);
    // 根据评论id和帖子作者id删除评论
    @Delete("DELETE FROM comments WHERE id = #{commentId} AND post_id IN" +
            " (SELECT id FROM posts WHERE user_id = #{userId})")
    int deleteCommentByAuthor(int commentId, int userId);
    @Select("SELECT id, content, user_id as userId, post_id as postId, " +
            "created_at as createdAt " +
            "FROM comments WHERE id = #{commentId}")
    Comments getCommentById(int commentId);
    // 根据评论id获取所属帖子id
    @Select("SELECT post_id FROM comments WHERE id = #{commentId}")
    Integer getPostIdByCommentId(int commentId);
    // 评论数减1
    @Update("UPDATE posts SET comment_count = comment_count - 1 WHERE id = #{postId}")
    void decrementCommentCount(int postId);

    //在首页根据帖子标题关键字和帖子类别关键字查询
    @Select("select * from posts where title like concat('%',#{keywords},'%') or category_name like concat('%',#{keywords},'%')")
    List<Posts> selectByKeywords(String keywords);
    //热搜实现
    @Select("select * from posts order by view_count desc limit 20")
    List<Posts> hotPosts();

    //按id查询类别id
    @Select("select id from categories where name=#{name}")
    int selectCategorieById(String name);

    @Select("select count(*) from comments where post_id=#{id}")
    public int selectCommentCount(int id);

    //按userId查询文章数据
    @Select("select view_count, like_count, comment_count, share_count from posts where user_id=#{userId} And type='文章'")
    List<Posts> selectData(int userId);



    // 动态插入in (x,x,x) 的方法 --> 遍历集合
    @Select({
            "<script>",
            "SELECT * FROM posts",
            "<where>",
            "   <if test='userIds != null and userIds.size() > 0'>",
            "       user_id IN",
            "       <foreach item='id' collection='userIds' open='(' separator=',' close=')'>",
            "           #{id}",
            "       </foreach>",
            "   </if>",
            "   <if test='userIds == null or userIds.size() == 0'>",
            "       1=0", // 处理空列表情况，避免返回所有数据
            "   </if>",
            "</where>",
            "ORDER BY created_at DESC", // 降序排列
            "</script>"
    })
    List<Posts> findPostsByUserIds(@Param("userIds") List<Integer> userIds);
}
